package online.longzipeng.mywebdemo.commen;

import lombok.Data;
import online.longzipeng.mywebdemo.commen.exception.ErrorCodeEnum;

import java.io.Serializable;

/**
 * 通用响应
 *
 * @author lzp
 */
@Data
public class Result<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 编码：0表示成功，其他值表示失败
	 */
	private int code = ErrorCodeEnum.SUCCESS.value;
	/**
	 * 消息内容
	 */
	private String msg = ErrorCodeEnum.SUCCESS.title;
	/**
	 * 响应数据
	 */
	private T data;

	public static <T> Result<T> error() {
		return generate(ErrorCodeEnum.ERROR.value, ErrorCodeEnum.ERROR.getTitle());
	}

	/**
	 * 快速生成返回结果
	 * @param code 状态码
	 * @param msg 对应消息内容
	 */
	public static <T> Result<T> generate(int code, String msg) {
		Result<T> result = new Result();
		result.setCode(code);
		result.setMsg(msg);
		return result;
	}

	public static <T> Result<T> success() {
		return new Result<>();
	}

	public static <T> Result<T> success(T data) {
		Result<T> result = new Result<>();
		result.setData(data);
		return result;
	}

}
